<!DOCTYPE html>
<html lang="zh-CN">
<head>
	    <meta http-equiv="X-UA-Compatible" content="IE=9;IE=8;IE=7" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>Python性能优化的20条建议 - code123</title>                                <meta name="keywords" content="Python" />
    <meta name="description" content="1、优化算法时间复杂度算法的时间复杂度对程序的执行效率影响最大，在Python中可以通过选择合适的数据结构来优化时间复杂度，如list和set查找某一个元素的时间复杂度分别是O(n)和O(1)。不同的场景有不同的优化方" />
    <meta name="viewport" content="initial-scale=1.0,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <link rel="Shortcut Icon" href="http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/uploads/2015/08/favicon.ico" type="image/x-icon" />
    <link rel="alternate" type="application/rss+xml" title="code123 &raquo; Feed" href="http://www.code123.cc/feed" />
<link rel="alternate" type="application/rss+xml" title="code123 &raquo; 评论Feed" href="http://www.code123.cc/comments/feed" />
<link rel="alternate" type="application/rss+xml" title="code123 &raquo; Python性能优化的20条建议评论Feed" href="http://www.code123.cc/2657.html/feed" />
		<script type="text/javascript">
			window._wpemojiSettings = {"baseUrl":"http:\/\/s.w.org\/images\/core\/emoji\/72x72\/","ext":".png","source":{"concatemoji":"http:\/\/www.code123.cc\/wp-includes\/js\/wp-emoji-release.min.js?ver=1462685583"}};
			!function(a,b,c){function d(a){var c=b.createElement("canvas"),d=c.getContext&&c.getContext("2d");return d&&d.fillText?(d.textBaseline="top",d.font="600 32px Arial","flag"===a?(d.fillText(String.fromCharCode(55356,56812,55356,56807),0,0),c.toDataURL().length>3e3):(d.fillText(String.fromCharCode(55357,56835),0,0),0!==d.getImageData(16,16,1,1).data[0])):!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g;c.supports={simple:d("simple"),flag:d("flag")},c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.simple&&c.supports.flag||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",g,!1),a.addEventListener("load",g,!1)):(a.attachEvent("onload",g),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
		</script>
		<style type="text/css">
img.wp-smiley,
img.emoji {
	display: inline !important;
	border: none !important;
	box-shadow: none !important;
	height: 1em !important;
	width: 1em !important;
	margin: 0 .07em !important;
	vertical-align: -0.1em !important;
	background: none !important;
	padding: 0 !important;
}
</style>
<link rel='stylesheet' id='dwqa-style-css'  href='css/style.css?ver=1462685583' type='text/css' media='all' />
<script type='text/javascript' src='http://cdn.staticfile.org/jquery/1.11.1/jquery.min.js?ver=1462685583'></script>
<script type='text/javascript' src='http://cdn.staticfile.org/jquery-migrate/1.2.1/jquery-migrate.min.js?ver=1462685583'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var CrayonSyntaxSettings = {"version":"2.6.5","is_admin":"0","ajaxurl":"http:\/\/www.code123.cc\/wp-admin\/admin-ajax.php","prefix":"crayon-","setting":"crayon-setting","selected":"crayon-setting-selected","changed":"crayon-setting-changed","special":"crayon-setting-special","orig_value":"data-orig-value","debug":""};
var CrayonSyntaxStrings = {"copy":"\u4f7f\u7528 %s \u590d\u5236\uff0c\u4f7f\u7528 %s \u7c98\u8d34\u3002","minimize":"Click To Expand Code"};
/* ]]> */
</script>
<script type='text/javascript' src='http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/plugins/crayon-syntax-highlighter/js/min/crayon.min.js?ver=1462685583'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var mirana = {"ajaxurl":"http:\/\/www.code123.cc\/wp-admin\/admin-ajax.php","swf":"http:\/\/www.code123.cc\/wp-content\/themes\/Mirana\/static\/swf","adminurl":"http:\/\/www.code123.cc\/wp-content\/themes\/Mirana","postid":"2657"};
/* ]]> */
</script>
<script type='text/javascript' src='http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/themes/Mirana/static/js/single.js?ver=1462685583'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var wpzan_ajax_url = "http:\/\/www.code123.cc\/wp-admin\/admin-ajax.php";
/* ]]> */
</script>
<script type='text/javascript' src='http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/plugins/wp-zan/static/js/wp-zan.js?ver=1462685583'></script>
<script type='text/javascript' src='http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/plugins/wp-favorite-posts/wpfp.js?ver=1462685583'></script>
<link rel='prev' title='开源数据库的现状：方案、设计思想及适用场景' href='http://www.code123.cc/2655.html' />
<link rel='next' title='Redis和Memcached的区别' href='http://www.code123.cc/2659.html' />
<link rel='canonical' href='http://www.code123.cc/2657.html' />
<link rel='shortlink' href='http://www.code123.cc/?p=2657' />
		<style type="text/css">
			.wp-zan{
				color: !important
			}
			.wp-zan:hover{
				color: !important
			}
			.wp-zan.zaned{
				color: !important
			}
		</style>
	    </head>
<body class="single single-post postid-2657 single-format-standard">
<header id="header">
	<div class="center">
        		<a class="logo" href="http://www.code123.cc" alt="code123"><img src="http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/uploads/2015/08/logo3.png" alt="code123" /></a>
		<nav id="topnav" class="menu-menu1-container"><ul id="menu-menu1" class="menu"><li id="menu-item-1454" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1454"><a href="http://www.code123.cc/">首页</a></li>
<li id="menu-item-269" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-269"><a href="http://www.code123.cc/category/trends">业界</a></li>
<li id="menu-item-1600" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-1600"><a href="http://www.code123.cc/category/startup">创业</a></li>
<li id="menu-item-1450" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-1450"><a href="http://www.code123.cc/category/interview-carrier">面试</a></li>
<li id="menu-item-1451" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-1451"><a href="http://www.code123.cc/category/java">Java</a></li>
<li id="menu-item-1452" class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-1452"><a href="http://www.code123.cc/category/python">Python</a></li>
</ul></nav>        <div class="search right">
            <form class="search-form clearfix" method="get" action="http://www.code123.cc/">
                <input class="search-submit" type="submit" />
                <input class="search-text" name="s" type="text" placeholder="Search..." />
            </form>
        </div>
	</div>
</header>
<div id="content" style="background:#f0f2f7">
<!--div><center><br>
                      <a href="http://my.yizhihongxing.com/aff.php?aff=494" target="_blank"><img src="http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/uploads/2016/07/vpn.png"></a>
                    </center></div-->
	<div class="center sincenter clearfix">
		<div id="container">
            <div id="singlepost">
			                <article class="post-singular">
    <!--<div class="post-illustration">
        <img src='http://www.code123.cc/wp-content/themes/Mirana/timthumb.php&#63;src=http://www.code123.cc/wp-content/themes/Mirana/static/images/random/2.jpg&#38;w=720&#38;h=450&#38;zc=1&#38;q=100' alt='Python性能优化的20条建议' width='720' height='450' />    </div>-->
    <div id="imagepost">
        <div class="post-body">
            <h1 class="sptitle"><a href="http://www.code123.cc/2657.html" rel="bookmark">Python性能优化的20条建议</a></h1>
            <section class="singleinfo">
                <ul class="postinfo">
                    <li class="posttime">作者：code123</li><li class="infoleft">|</li>
                    <li class="posttime">时间：2016-07-16</li><li class="infoleft">|</li>
                    <li class="postviews">浏览：679 views</li><li class="infoleft">|</li>
                    <li class="postcat">分类：<a href="http://www.code123.cc/category/python" rel="category tag">Python</a></li>
                </ul>
            </section>
            <div class="postcont">
                <p>1、优化算法时间复杂度</p>
<p>算法的时间复杂度对程序的执行效率影响最大，在Python中可以通过选择合适的数据结构来优化时间复杂度，如list和set查找某一个元素的时间复杂度分别是O(n)和O(1)。不同的场景有不同的优化方式，总得来说，一般有分治，分支界限，贪心，动态规划等思想。</p>
<p>2、减少冗余数据</p>
<p>如用上三角或下三角的方式去保存一个大的对称矩阵。在0元素占大多数的矩阵里使用稀疏矩阵表示。</p>
<p>3、合理使用copy与deepcopy</p>
<p>对于dict和list等数据结构的对象，直接赋值使用的是引用的方式。而有些情况下需要复制整个对象，这时可以使用copy包里的copy和deepcopy，这两个函数的不同之处在于后者是递归复制的。效率也不一样：（以下程序在ipython中运行）</p>
<blockquote><p><span class="">import</span> <span class="">copy</span></p>
<p><span class="">a</span><span class=""> = </span><span class="">range</span><span class="">(</span><span class="">100000</span><span class="">)</span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10</span> <span class="">copy</span><span class="">.</span><span class="">copy</span><span class="">(</span><span class="">a</span><span class="">)</span> <span class=""># 运行10次 copy.copy(a)</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10</span> <span class="">copy</span><span class="">.</span><span class="">deepcopy</span><span class="">(</span><span class="">a</span><span class="">)</span></p>

<p><span class="">10</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">1.55</span> <span class="">ms per </span><span class="">loop</span></p>
<p><span class="">10</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">151</span> <span class="">ms per </span><span class="">loop</span></p></blockquote>
<p>timeit后面的-n表示运行的次数，后两行对应的是两个timeit的输出，下同。由此可见后者慢一个数量级。</p>
<p>4、使用dict或set查找元素</p>
<p>python dict和set都是使用hash表来实现(类似c++11标准库中unordered_map)，查找元素的时间复杂度是O(1)</p>
<blockquote><p><span class="">a</span><span class=""> = </span><span class="">range</span><span class="">(</span><span class="">1000</span><span class="">)</span></p>
<p><span class="">s</span><span class=""> = </span><span class="">set</span><span class="">(</span><span class="">a</span><span class="">)</span></p>
<p><span class="">d</span><span class=""> = </span><span class="">dict</span><span class="">((</span><span class="">i</span><span class="">,</span><span class="">1</span><span class="">)</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">a</span><span class="">)</span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10000</span> <span class="">100</span> <span class="">in</span> <span class="">d</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10000</span> <span class="">100</span> <span class="">in</span> <span class="">s</span></p>

<p><span class="">10000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">43.5</span> <span class="">ns per </span><span class="">loop</span></p>
<p><span class="">10000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">49.6</span> <span class="">ns per </span><span class="">loop</span></p></blockquote>
<p>dict的效率略高(占用的空间也多一些)。</p>
<p>5、合理使用生成器（generator）和yield</p>
<blockquote><p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">a</span><span class=""> = </span><span class="">(</span><span class="">i</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">range</span><span class="">(</span><span class="">100000</span><span class="">))</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">b</span><span class=""> = </span><span class="">[</span><span class="">i</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">range</span><span class="">(</span><span class="">100000</span><span class="">)]</span></p>

<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">1.54</span> <span class="">ms per </span><span class="">loop</span></p>
<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">4.56</span> <span class="">ms per </span><span class="">loop</span></p></blockquote>
<p>使用()得到的是一个generator对象，所需要的内存空间与列表的大小无关，所以效率会高一些。在具体应用上，比如set(i for i in range(100000))会比set([i for i in range(100000)])快。</p>
<p>但是对于需要循环遍历的情况：</p>
<blockquote><p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10</span> <span class="">for</span> <span class="">x</span> <span class="">in</span> <span class="">(</span><span class="">i</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">range</span><span class="">(</span><span class="">100000</span><span class="">))</span><span class="">: </span><span class="">pass</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10</span> <span class="">for</span> <span class="">x</span> <span class="">in</span> <span class="">[</span><span class="">i</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">range</span><span class="">(</span><span class="">100000</span><span class="">)]</span><span class="">: </span><span class="">pass</span></p>

<p><span class="">10</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">6.51</span> <span class="">ms per </span><span class="">loop</span></p>
<p><span class="">10</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">5.54</span> <span class="">ms per </span><span class="">loop</span></p></blockquote>
<p>后者的效率反而更高，但是如果循环里有break,用generator的好处是显而易见的。yield也是用于创建generator：</p>
<blockquote><p><span class="">def</span> <span class="">yield_func</span><span class="">(</span><span class="">ls</span><span class="">)</span><span class="">:</span></p>
<p><span class="">    </span><span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">ls</span><span class="">:</span></p>
<p><span class="">        </span><span class="">yield</span> <span class="">i</span><span class="">+</span><span class="">1</span></p>

<p><span class="">def</span> <span class="">not_yield_func</span><span class="">(</span><span class="">ls</span><span class="">)</span><span class="">:</span></p>
<p><span class="">    </span><span class="">return</span> <span class="">[</span><span class="">i</span><span class="">+</span><span class="">1</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">ls</span><span class="">]</span></p>

<p><span class="">ls</span><span class=""> = </span><span class="">range</span><span class="">(</span><span class="">1000000</span><span class="">)</span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">yield_func</span><span class="">(</span><span class="">ls</span><span class="">)</span><span class="">:</span><span class="">pass</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">not_yield_func</span><span class="">(</span><span class="">ls</span><span class="">)</span><span class="">:</span><span class="">pass</span></p>

<p><span class="">10</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">63.8</span> <span class="">ms per </span><span class="">loop</span></p>
<p><span class="">10</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">62.9</span> <span class="">ms per </span><span class="">loop</span></p></blockquote>
<p>对于内存不是非常大的list，可以直接返回一个list，但是可读性yield更佳(人个喜好)。</p>
<p>python2.x内置generator功能的有xrange函数、itertools包等。</p>
<p>6、优化循环</p>
<p>循环之外能做的事不要放在循环内，比如下面的优化可以快一倍：</p>
<blockquote><p><span class="">a</span><span class=""> = </span><span class="">range</span><span class="">(</span><span class="">10000</span><span class="">)</span></p>
<p><span class="">size_a</span><span class=""> = </span><span class="">len</span><span class="">(</span><span class="">a</span><span class="">)</span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">1000</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">a</span><span class="">: </span><span class="">k</span><span class=""> = </span><span class="">len</span><span class="">(</span><span class="">a</span><span class="">)</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">1000</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">a</span><span class="">: </span><span class="">k</span><span class=""> = </span><span class="">size</span><span class="">_</span>a</p>

<p><span class="">1000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">569</span> µ<span class="">s</span> <span class="">per </span><span class="">loop</span></p>
<p><span class="">1000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">256</span> µ<span class="">s</span> <span class="">per </span><span class="">loop</span></p></blockquote>
<p>7、优化包含多个判断表达式的顺序</p>
<p>对于and，应该把满足条件少的放在前面，对于or，把满足条件多的放在前面。如：</p>
<blockquote><p><span class="">a</span><span class=""> = </span><span class="">range</span><span class="">(</span><span class="">2000</span><span class="">)</span><span class="">  </span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">[</span><span class="">i</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">a</span> <span class="">if</span> <span class="">10</span><span class=""> &lt; </span><span class="">i</span><span class=""> &lt; </span><span class="">20</span> <span class="">or</span> <span class="">1000</span><span class=""> &lt; </span><span class="">i</span><span class=""> &lt; </span><span class="">2000</span><span class="">]</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">[</span><span class="">i</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">a</span> <span class="">if</span> <span class="">1000</span><span class=""> &lt; </span><span class="">i</span><span class=""> &lt; </span><span class="">2000</span> <span class="">or</span> <span class="">100</span><span class=""> &lt; </span><span class="">i</span><span class=""> &lt; </span><span class="">20</span><span class="">]</span><span class="">    </span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">[</span><span class="">i</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">a</span> <span class="">if</span> <span class="">i</span><span class=""> % </span><span class="">2</span><span class=""> == </span><span class="">0</span> <span class="">and</span> <span class="">i</span><span class=""> &gt; </span><span class="">1900</span><span class="">]</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">[</span><span class="">i</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">a</span> <span class="">if</span> <span class="">i</span><span class=""> &gt; </span><span class="">1900</span> <span class="">and</span> <span class="">i</span><span class=""> % </span><span class="">2</span><span class=""> == </span><span class="">0</span><span class="">]</span></p>

<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">287</span> µ<span class="">s</span> <span class="">per </span><span class="">loop</span></p>
<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">214</span> µ<span class="">s</span> <span class="">per </span><span class="">loop</span></p>
<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">128</span> µ<span class="">s</span> <span class="">per </span><span class="">loop</span></p>
<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">56.1</span> µ<span class="">s</span> <span class="">per </span><span class="">loop</span></p></blockquote>
<p>8、使用join合并迭代器中的字符串</p>
<blockquote><p><span class="">In</span> <span class="">[</span><span class="">1</span><span class="">]</span><span class="">: %%</span><span class="">timeit</span></p>
<p><span class="">   </span><span class="">…</span><span class="">: </span><span class="">s</span><span class=""> = </span><span class="">”</span></p>
<p><span class="">   </span><span class="">…</span><span class="">: </span><span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">a</span><span class="">:</span></p>
<p><span class="">   </span><span class="">…</span><span class="">:         </span><span class="">s</span><span class=""> += </span><span class="">i</span></p>
<p><span class="">   </span><span class="">…</span><span class="">:</span></p>
<p><span class="">10000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">59.8</span> µ<span class="">s</span> <span class="">per loop</span></p>

<p><span class="">In</span> <span class="">[</span><span class="">2</span><span class="">]</span><span class="">: %%</span><span class="">timeit</span></p>
<p><span class="">s</span><span class=""> = </span><span class="">”</span><span class="">.</span><span class="">join</span><span class="">(</span><span class="">a</span><span class="">)</span></p>
<p><span class="">   </span><span class="">…</span><span class="">:</span></p>
<p><span class="">100000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">11.8</span> µ<span class="">s</span> <span class="">per </span><span class="">loop</span></p></blockquote>
<p>join对于累加的方式，有大约5倍的提升。</p>
<p>9、选择合适的格式化字符方式</p>
<blockquote><p><span class="">s1</span><span class="">,</span> <span class="">s2</span><span class=""> = </span><span class="">‘ax’</span><span class="">,</span> <span class="">‘bx’</span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100000</span> <span class="">‘abc%s%s’</span><span class=""> % </span><span class="">(</span><span class="">s1</span><span class="">,</span> <span class="">s2</span><span class="">)</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100000</span> <span class="">‘abc{0}{1}’</span><span class="">.</span><span class="">format</span><span class="">(</span><span class="">s1</span><span class="">,</span> <span class="">s2</span><span class="">)</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100000</span> <span class="">‘abc’</span><span class=""> + </span><span class="">s1</span><span class=""> + </span><span class="">s2</span></p>

<p><span class="">100000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">183</span> <span class="">ns per </span><span class="">loop</span></p>
<p><span class="">100000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">169</span> <span class="">ns per </span><span class="">loop</span></p>
<p><span class="">100000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">103</span> <span class="">ns per </span><span class="">loop</span></p></blockquote>
<p>三种情况中，%的方式是最慢的，但是三者的差距并不大（都非常快）。(个人觉得%的可读性最好)</p>
<p>10、不借助中间变量交换两个变量的值</p>
<blockquote><p><span class="">In</span> <span class="">[</span><span class="">3</span><span class="">]</span><span class="">: %%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10000</span></p>
<p><span class="">    </span><span class="">a</span><span class="">,</span><span class="">b</span><span class="">=</span><span class="">1</span><span class="">,</span><span class="">2</span></p>
<p><span class="">   </span><span class="">….</span><span class="">: </span><span class="">c</span><span class="">=</span><span class="">a</span><span class="">;</span><span class="">a</span><span class="">=</span><span class="">b</span><span class="">;</span><span class="">b</span><span class="">=</span><span class="">c</span><span class="">;</span></p>
<p><span class="">   </span><span class="">….</span><span class="">:</span></p>
<p><span class="">10000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">172</span> <span class="">ns per loop</span></p>

<p><span class="">In</span> <span class="">[</span><span class="">4</span><span class="">]</span><span class="">: %%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10000</span></p>
<p><span class="">a</span><span class="">,</span><span class="">b</span><span class="">=</span><span class="">1</span><span class="">,</span><span class="">2</span></p>
<p><span class="">a</span><span class="">,</span><span class="">b</span><span class="">=</span><span class="">b</span><span class="">,</span><span class="">a</span></p>
<p><span class="">   </span><span class="">….</span><span class="">:</span></p>
<p><span class="">10000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">86</span> <span class="">ns per </span><span class="">loop</span></p></blockquote>
<p>使用a,b=b,a而不是c=a;a=b;b=c;来交换a,b的值，可以快1倍以上。</p>
<p>11、使用if is</p>
<blockquote><p><span class="">a</span><span class=""> = </span><span class="">range</span><span class="">(</span><span class="">10000</span><span class="">)</span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">[</span><span class="">i</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">a</span> <span class="">if</span> <span class="">i</span><span class=""> == </span><span class="">True</span><span class="">]</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">[</span><span class="">i</span> <span class="">for</span> <span class="">i</span> <span class="">in</span> <span class="">a</span> <span class="">if</span> <span class="">i</span> <span class="">is</span> <span class="">True</span><span class="">]</span></p>

<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">531</span> µ<span class="">s</span> <span class="">per </span><span class="">loop</span></p>
<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">362</span> µ<span class="">s</span> <span class="">per </span><span class="">loop</span></p></blockquote>
<p>使用 if is True 比 if == True 将近快一倍。</p>
<p>12、使用级联比较x &lt; y &lt; z</p>
<blockquote><p><span class="">x</span><span class="">,</span> <span class="">y</span><span class="">,</span> <span class="">z</span><span class=""> = </span><span class="">1</span><span class="">,</span><span class="">2</span><span class="">,</span><span class="">3</span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">1000000</span> <span class="">if</span> <span class="">x</span><span class=""> &lt; </span><span class="">y</span><span class=""> &lt; </span><span class="">z</span><span class="">:</span><span class="">pass</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">1000000</span> <span class="">if</span> <span class="">x</span><span class=""> &lt; </span><span class="">y</span> <span class="">and</span> <span class="">y</span><span class=""> &lt; </span><span class="">z</span><span class="">:</span><span class="">pass</span></p>

<p><span class="">1000000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">101</span> <span class="">ns per </span><span class="">loop</span></p>
<p><span class="">1000000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">121</span> <span class="">ns per </span><span class="">loop</span></p></blockquote>
<p>x &lt; y &lt; z效率略高，而且可读性更好。</p>
<p>13、while 1 比 while True 更快</p>
<blockquote><p><span class="">def</span> <span class="">while_1</span><span class="">()</span><span class="">:</span></p>
<p><span class="">    </span><span class="">n</span><span class=""> = </span><span class="">100000</span></p>
<p><span class="">    </span><span class="">while</span> <span class="">1</span><span class="">:</span></p>
<p><span class="">        </span><span class="">n</span><span class=""> -= </span><span class="">1</span></p>
<p><span class="">        </span><span class="">if</span> <span class="">n</span><span class=""> &lt;= </span><span class="">0</span><span class="">: </span><span class="">break</span></p>
<p><span class="">def</span> <span class="">while_true</span><span class="">()</span><span class="">:</span></p>
<p><span class="">    </span><span class="">n</span><span class=""> = </span><span class="">100000</span></p>
<p><span class="">    </span><span class="">while</span> <span class="">True</span><span class="">:</span></p>
<p><span class="">        </span><span class="">n</span><span class=""> -= </span><span class="">1</span></p>
<p><span class="">        </span><span class="">if</span> <span class="">n</span><span class=""> &lt;= </span><span class="">0</span><span class="">: </span><span class="">break</span><span class="">    </span></p>

<p><span class="">m</span><span class="">,</span> <span class="">n</span><span class=""> = </span><span class="">1000000</span><span class="">,</span> <span class="">1000000</span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">while_1</span><span class="">()</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">while_true</span><span class="">()</span></p>

<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">3.69</span> <span class="">ms per </span><span class="">loop</span></p>
<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">5.61</span> <span class="">ms per </span><span class="">loop</span></p></blockquote>
<p>while 1 比 while true快很多，原因是在python2.x中，True是一个全局变量，而非关键字。</p>
<p>14、使用**而不是pow</p>
<blockquote><p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10000</span> <span class="">c</span><span class=""> = </span><span class="">pow</span><span class="">(</span><span class="">2</span><span class="">,</span><span class="">20</span><span class="">)</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">10000</span> <span class="">c</span><span class=""> = </span><span class="">2</span><span class="">**</span><span class="">20</span></p>

<p><span class="">10000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">284</span> <span class="">ns per </span><span class="">loop</span></p>
<p><span class="">10000</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">16.9</span> <span class="">ns per </span><span class="">loop</span></p></blockquote>
<p>**就是快10倍以上！</p>
<p>15、使用 cProfile, cStringIO 和 cPickle等用c实现相同功能（分别对应profile, StringIO, pickle）的包</p>
<blockquote><p><span class="">import</span> <span class="">cPickle</span></p>
<p><span class="">import</span> <span class="">pickle</span></p>
<p><span class="">a</span><span class=""> = </span><span class="">range</span><span class="">(</span><span class="">10000</span><span class="">)</span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">x</span><span class=""> = </span><span class="">cPickle</span><span class="">.</span><span class="">dumps</span><span class="">(</span><span class="">a</span><span class="">)</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">x</span><span class=""> = </span><span class="">pickle</span><span class="">.</span><span class="">dumps</span><span class="">(</span><span class="">a</span><span class="">)</span></p>

<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">1.58</span> <span class="">ms per </span><span class="">loop</span></p>
<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">17</span> <span class="">ms per </span><span class="">loop</span></p></blockquote>
<p>由c实现的包，速度快10倍以上！</p>
<p>16、使用最佳的反序列化方式</p>
<p>下面比较了eval, cPickle, json方式三种对相应字符串反序列化的效率：</p>
<blockquote><p><span class="">import</span> <span class="">json</span></p>
<p><span class="">import</span> <span class="">cPickle</span></p>
<p><span class="">a</span><span class=""> = </span><span class="">range</span><span class="">(</span><span class="">10000</span><span class="">)</span></p>
<p><span class="">s1</span><span class=""> = </span><span class="">str</span><span class="">(</span><span class="">a</span><span class="">)</span></p>
<p><span class="">s2</span><span class=""> = </span><span class="">cPickle</span><span class="">.</span><span class="">dumps</span><span class="">(</span><span class="">a</span><span class="">)</span></p>
<p><span class="">s3</span><span class=""> = </span><span class="">json</span><span class="">.</span><span class="">dumps</span><span class="">(</span><span class="">a</span><span class="">)</span></p>
<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">x</span><span class=""> = </span><span class="">eval</span><span class="">(</span><span class="">s1</span><span class="">)</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">x</span><span class=""> = </span><span class="">cPickle</span><span class="">.</span><span class="">loads</span><span class="">(</span><span class="">s2</span><span class="">)</span></p>

<p><span class="">%</span><span class="">timeit</span><span class=""> –</span><span class="">n</span> <span class="">100</span> <span class="">x</span><span class=""> = </span><span class="">json</span><span class="">.</span><span class="">loads</span><span class="">(</span><span class="">s3</span><span class="">)</span></p>

<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">16.8</span> <span class="">ms per </span><span class="">loop</span></p>
<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">2.02</span> <span class="">ms per </span><span class="">loop</span></p>
<p><span class="">100</span> <span class="">loops</span><span class="">,</span> <span class="">best </span><span class="">of</span> <span class="">3</span><span class="">: </span><span class="">798</span> µ<span class="">s</span> <span class="">per </span><span class="">loop</span></p></blockquote>
<p>可见json比cPickle快近3倍，比eval快20多倍。</p>
<p>17、使用C扩展(Extension)</p>
<p>目前主要有CPython(python最常见的实现的方式)原生API, ctypes,Cython，cffi三种方式，它们的作用是使得Python程序可以调用由C编译成的动态链接库，其特点分别是：</p>
<p><strong>CPython原生API</strong>: 通过引入Python.h头文件，对应的C程序中可以直接使用Python的数据结构。实现过程相对繁琐，但是有比较大的适用范围。</p>
<p><strong>ctypes</strong>: 通常用于封装(wrap)C程序，让纯Python程序调用动态链接库（Windows中的dll或Unix中的so文件）中的函数。如果想要在python中使用已经有C类库，使用ctypes是很好的选择，有一些基准测试下，python2+ctypes是性能最好的方式。</p>
<p><strong>Cython</strong>: Cython是CPython的超集，用于简化编写C扩展的过程。Cython的优点是语法简洁，可以很好地兼容numpy等包含大量C扩展的库。Cython的使得场景一般是针对项目中某个算法或过程的优化。在某些测试中，可以有几百倍的性能提升。</p>
<p><strong>cffi</strong>: cffi的就是ctypes在pypy（详见下文）中的实现，同进也兼容CPython。cffi提供了在python使用C类库的方式，可以直接在python代码中编写C代码，同时支持链接到已有的C类库。</p>
<p>使用这些优化方式一般是针对已有项目性能瓶颈模块的优化，可以在少量改动原有项目的情况下大幅度地提高整个程序的运行效率。</p>
<p>18、并行编程</p>
<p>因为GIL的存在，Python很难充分利用多核CPU的优势。但是，可以通过内置的模块multiprocessing实现下面几种并行模式：</p>
<p><strong>多进程</strong>：对于CPU密集型的程序，可以使用multiprocessing的Process,Pool等封装好的类，通过多进程的方式实现并行计算。但是因为进程中的通信成本比较大，对于进程之间需要大量数据交互的程序效率未必有大的提高。</p>
<p><strong>多线程</strong>：对于IO密集型的程序，multiprocessing.dummy模块使用multiprocessing的接口封装threading，使得多线程编程也变得非常轻松(比如可以使用Pool的map接口，简洁高效)。</p>
<p><strong>分布式</strong>：multiprocessing中的Managers类提供了可以在不同进程之共享数据的方式，可以在此基础上开发出分布式的程序。</p>
<p>不同的业务场景可以选择其中的一种或几种的组合实现程序性能的优化。</p>
<p>19、终级大杀器：PyPy</p>
<p>PyPy是用RPython(CPython的子集)实现的Python，根据官网的基准测试数据，它比CPython实现的Python要快6倍以上。快的原因是使用了Just-in-Time(JIT)编译器，即动态编译器，与静态编译器(如gcc,javac等)不同，它是利用程序运行的过程的数据进行优化。由于历史原因，目前pypy中还保留着GIL，不过正在进行的STM项目试图将PyPy变成没有GIL的Python。</p>
<p>如果python程序中含有C扩展(非cffi的方式)，JIT的优化效果会大打折扣，甚至比CPython慢（比Numpy）。所以在PyPy中最好用纯Python或使用cffi扩展。</p>
<p>随着STM，Numpy等项目的完善，相信PyPy将会替代CPython。</p>
<p>20、使用性能分析工具</p>
<p>除了上面在ipython使用到的timeit模块，还有cProfile。cProfile的使用方式也非常简单： python -m cProfile filename.py，filename.py 是要运行程序的文件名，可以在标准输出中看到每一个函数被调用的次数和运行的时间，从而找到程序的性能瓶颈，然后可以有针对性地优化。</p>
<p>参考</p>
<blockquote><p>[1] http://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/</p>
<p>[2] http://maxburstein.com/blog/speeding-up-your-python-code/</p></blockquote>
            </div>
           <!--div><center>
           <a href="http://my.yizhihongxing.com/aff.php?aff=494" target="_blank"><img src="http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/uploads/2016/07/VPN4.png"></a>
           </center></div><br-->
<div class="postcont"><hr>
上一篇: <a href="http://www.code123.cc/2655.html" rel="prev">开源数据库的现状：方案、设计思想及适用场景</a><br>
下一篇: <a href="http://www.code123.cc/2659.html" rel="next">Redis和Memcached的区别</a></div>
        </div>
    </div>
</article>
			
		    <div class="singinfo">
	                <div class="posttags">标签：<a href="http://www.code123.cc/tag/python" rel="tag">Python</a></div>
	                <div class="postzan2"><a id="wp-zan-2657" class="wp-zan" onclick="wpzan(2657, 0)" href="javascript:;"><span>8</span></a> <span class="zanlike">Likes</span></div>
	            </div>
            </div><br>
            <ul id="related-posts" class="clearfix"><h3>你或许也喜欢</h3>            <li class="left related-post">
                <a href="http://www.code123.cc/2285.html " class="related-post-image" rel="nofollow"><img src='http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/uploads/2015/11/python-tricks.jpg?imageView/1/w/150/h/110/q/100' alt='Python奇技淫巧' width='150' height='110' /></a>
                <a class="related-post-tittle" href="http://www.code123.cc/2285.html" >Python奇技淫巧</a>
            </li>
                    <li class="left related-post">
                <a href="http://www.code123.cc/2258.html " class="related-post-image" rel="nofollow"><img src='http://www.code123.cc/wp-content/themes/Mirana/timthumb.php&#63;src=http://www.code123.cc/wp-content/themes/Mirana/static/images/random/3.jpg&#38;w=150&#38;h=110&#38;zc=1&#38;q=100' alt='tornado异步请求' width='150' height='110' /></a>
                <a class="related-post-tittle" href="http://www.code123.cc/2258.html" >tornado异步请求</a>
            </li>
                    <li class="left related-post">
                <a href="http://www.code123.cc/1782.html " class="related-post-image" rel="nofollow"><img src='http://www.code123.cc/wp-content/themes/Mirana/timthumb.php&#63;src=http://www.code123.cc/wp-content/themes/Mirana/static/images/random/1.jpg&#38;w=150&#38;h=110&#38;zc=1&#38;q=100' alt='Python汉字转换成拼音' width='150' height='110' /></a>
                <a class="related-post-tittle" href="http://www.code123.cc/1782.html" >Python汉字转换成拼音</a>
            </li>
                    <li class="left related-post related-post-last">
                <a href="http://www.code123.cc/1757.html " class="related-post-image" rel="nofollow"><img src='http://www.code123.cc/wp-content/themes/Mirana/timthumb.php&#63;src=http://www.code123.cc/wp-content/themes/Mirana/static/images/random/9.jpg&#38;w=150&#38;h=110&#38;zc=1&#38;q=100' alt='Python WSGI详解' width='150' height='110' /></a>
                <a class="related-post-tittle" href="http://www.code123.cc/1757.html" >Python WSGI详解</a>
            </li>
        </ul><br />
		<div id="comments">

	

		

		
			<div id="respond">

					<form action="http://www.code123.cc/wp-comments-post.php" method="post" id="commentform" name="comm_frm">
						
							<!--未登录-->
							<div class="welcomediv">
																	<div class="welcometip">你目前的身份是游客，评论请输入昵称和电邮！</div>
															</div>

							<section id="comboxinfo">
								<div class="cominfodiv cominfodiv-author"><label for="author" class="author">昵 称：</label><input type="text" name="author" id="author" value="" tabindex="1" /></div>
								<div class="cominfodiv cominfodiv-email"><label for="email" class="email">邮 箱：</label><input type="email" name="email" id="email" value="" tabindex="2" /></div>
								<div class="cominfodiv cominfodiv-url"><label for="url" class="url">网 址：</label><input type="text" name="url" id="url" value="" tabindex="3" /></div>
							</section>
													
						<div id="text-area">
							<textarea name="comment" id="comment" rows="10" tabindex="4" placeholder="输入评论内容..." onkeydown="if(event.ctrlKey&&event.keyCode==13){document.getElementById('submit').click();return false};"></textarea>
						</div>

						<div class="submitdiv clearfix">
							<div class="submitcom"><input name="submit" type="submit" id="submit" tabindex="5" value="提交评论" /><input type='hidden' name='comment_post_ID' value='2657' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</div>
							<div id="cancel_comment_reply"><a rel="nofollow" id="cancel-comment-reply-link" href="/2657.html#respond" style="display:none;">取消回复</a></div>
						</div>
						<p style="display: none;"><input type="hidden" id="akismet_comment_nonce" name="akismet_comment_nonce" value="ed25ee9df5" /></p><p style="display: none;"><input type="hidden" id="ak_js" name="ak_js" value="48"/></p>
					</form>

			</div><!--end respond-->

		
</div><!--end comments-->          </div>
		<aside id="sidebar">         <section id="randomposts" class="div1">
                <h3>系列文章</h3>
                <ul class="romposts">
                      
                         
                            <li ><a href="http://www.code123.cc/255.html" title="Python正则表达式指南">Python正则表达式指南</a></li>
                     
                            <li ><a href="http://www.code123.cc/362.html" title="用c++扩展python">用c++扩展python</a></li>
                     
                            <li ><a href="http://www.code123.cc/365.html" title="Python脚本后台运行">Python脚本后台运行</a></li>
                     
                            <li ><a href="http://www.code123.cc/413.html" title="十个我希望早点知道的python方法">十个我希望早点知道的python方法</a></li>
                     
                            <li ><a href="http://www.code123.cc/420.html" title="怎样写一个模板引擎">怎样写一个模板引擎</a></li>
                     
                            <li ><a href="http://www.code123.cc/530.html" title="图说PHP/Java/Python/C的优缺点">图说PHP/Java/Python/C的优缺点</a></li>
                     
                            <li ><a href="http://www.code123.cc/1424.html" title="Python性能优化的20条建议">Python性能优化的20条建议</a></li>
                     
                            <li ><a href="http://www.code123.cc/1757.html" title="Python WSGI详解">Python WSGI详解</a></li>
                     
                            <li ><a href="http://www.code123.cc/1782.html" title="Python汉字转换成拼音">Python汉字转换成拼音</a></li>
                     
                            <li ><a href="http://www.code123.cc/2258.html" title="tornado异步请求">tornado异步请求</a></li>
                     
                            <li ><a href="http://www.code123.cc/2285.html" title="Python奇技淫巧">Python奇技淫巧</a></li>
                     
                            <li ><a href="http://www.code123.cc/2657.html" title="Python性能优化的20条建议">Python性能优化的20条建议</a></li>
                                    </ul>
            </section>
                    <section id="randomposts" class="fixrecent">
                <h3>最新文章</h3>
                <ul class="romposts">
                      
                         
                            <li><a href="http://www.code123.cc/2671.html" title="创业者应该知道的SAAS服务">创业者应该知道的SAAS服务</a></li>
                     
                            <li><a href="http://www.code123.cc/2659.html" title="Redis和Memcached的区别">Redis和Memcached的区别</a></li>
                     
                            <li><a href="http://www.code123.cc/2657.html" title="Python性能优化的20条建议">Python性能优化的20条建议</a></li>
                     
                            <li><a href="http://www.code123.cc/2655.html" title="开源数据库的现状：方案、设计思想及适用场景">开源数据库的现状：方案、设计思想及适用场景</a></li>
                     
                            <li><a href="http://www.code123.cc/2651.html" title="消息队列设计精要">消息队列设计精要</a></li>
                     
                            <li><a href="http://www.code123.cc/2648.html" title="Shell脚本编程总结及速查手册">Shell脚本编程总结及速查手册</a></li>
                                    </ul>
            </section>
        			<section id="sidcat">
				<h3>文章分类</h3>
				<ul>	<li class="cat-item cat-item-122"><a href="http://www.code123.cc/category/android" >Android</a> 22
</li>
	<li class="cat-item cat-item-171"><a href="http://www.code123.cc/category/django" >Django</a> 19
</li>
	<li class="cat-item cat-item-170"><a href="http://www.code123.cc/category/http" >HTTP</a> 5
</li>
	<li class="cat-item cat-item-232"><a href="http://www.code123.cc/category/ios" >iOS</a> 10
</li>
	<li class="cat-item cat-item-206"><a href="http://www.code123.cc/category/it-carrier" >IT职场</a> 14
</li>
	<li class="cat-item cat-item-108"><a href="http://www.code123.cc/category/java" >Java</a> 37
</li>
	<li class="cat-item cat-item-146"><a href="http://www.code123.cc/category/javascript" >JavaScript</a> 1
</li>
	<li class="cat-item cat-item-185"><a href="http://www.code123.cc/category/java-level02" >Java进阶</a> 2
</li>
	<li class="cat-item cat-item-293"><a href="http://www.code123.cc/category/leetcode" >LeetCode</a> 8
</li>
	<li class="cat-item cat-item-124"><a href="http://www.code123.cc/category/linux" >Linux</a> 5
</li>
	<li class="cat-item cat-item-140"><a href="http://www.code123.cc/category/mysql" >MySQL</a> 2
</li>
	<li class="cat-item cat-item-178"><a href="http://www.code123.cc/category/php" >PHP</a> 1
</li>
	<li class="cat-item cat-item-111"><a href="http://www.code123.cc/category/python" >Python</a> 28
</li>
	<li class="cat-item cat-item-322"><a href="http://www.code123.cc/category/redis" >Redis</a> 2
</li>
	<li class="cat-item cat-item-188"><a href="http://www.code123.cc/category/ruby" >Ruby</a> 1
</li>
	<li class="cat-item cat-item-109"><a href="http://www.code123.cc/category/tutor-scalar" >Scala教程</a> 3
</li>
	<li class="cat-item cat-item-134"><a href="http://www.code123.cc/category/spark" >Spark</a> 13
</li>
	<li class="cat-item cat-item-204"><a href="http://www.code123.cc/category/tcp-ip" >TCP/IP协议</a> 16
</li>
	<li class="cat-item cat-item-132"><a href="http://www.code123.cc/category/web-dev" >Web开发</a> 11
</li>
	<li class="cat-item cat-item-3"><a href="http://www.code123.cc/category/wiki" >wiki</a> 4
</li>
	<li class="cat-item cat-item-69"><a href="http://www.code123.cc/category/trends" >业界/资讯</a> 60
</li>
	<li class="cat-item cat-item-72"><a href="http://www.code123.cc/category/people" >人物</a> 8
</li>
	<li class="cat-item cat-item-113"><a href="http://www.code123.cc/category/startup" >创业</a> 21
</li>
	<li class="cat-item cat-item-138"><a href="http://www.code123.cc/category/bigdata" >大数据</a> 29
</li>
	<li class="cat-item cat-item-102"><a href="http://www.code123.cc/category/math-programming" >学点数学</a> 47
</li>
	<li class="cat-item cat-item-114"><a href="http://www.code123.cc/category/tools" >实用工具</a> 11
</li>
	<li class="cat-item cat-item-127"><a href="http://www.code123.cc/category/funny" >有趣</a> 12
</li>
	<li class="cat-item cat-item-142"><a href="http://www.code123.cc/category/architecture" >架构设计</a> 13
</li>
	<li class="cat-item cat-item-123"><a href="http://www.code123.cc/category/games-dev" >游戏开发</a> 1
</li>
	<li class="cat-item cat-item-115"><a href="http://www.code123.cc/category/interview-carrier" >程序员面试</a> 124
</li>
	<li class="cat-item cat-item-133"><a href="http://www.code123.cc/category/algorithm-datastruct" >算法/数据结构</a> 21
</li>
	<li class="cat-item cat-item-117"><a href="http://www.code123.cc/category/experience" >经验分享</a> 26
</li>
	<li class="cat-item cat-item-130"><a href="http://www.code123.cc/category/network-program" >网络编程</a> 4
</li>
	<li class="cat-item cat-item-129"><a href="http://www.code123.cc/category/computer-inside" >计算机原理</a> 12
</li>
	<li class="cat-item cat-item-119"><a href="http://www.code123.cc/category/design" >设计</a> 7
</li>
	<li class="cat-item cat-item-257"><a href="http://www.code123.cc/category/design-mode" >设计模式</a> 7
</li>
</ul>
			</section>
		    <section id="tags" class="fixtags">
        <h3>网站标签</h3>
        <div class="sidtags clearfix">
            <a href='http://www.code123.cc/tag/google-ctci' class='tag-link-181' title='109个话题' style='font-size: 14px;'>谷歌面试</a>
<a href='http://www.code123.cc/tag/experience' class='tag-link-100' title='38个话题' style='font-size: 14px;'>经验</a>
<a href='http://www.code123.cc/tag/internet' class='tag-link-71' title='37个话题' style='font-size: 14px;'>互联网</a>
<a href='http://www.code123.cc/tag/startup' class='tag-link-70' title='31个话题' style='font-size: 14px;'>创业</a>
<a href='http://www.code123.cc/tag/tech-trends' class='tag-link-268' title='29个话题' style='font-size: 14px;'>浪潮之巅</a>
<a href='http://www.code123.cc/tag/beauty-of-math' class='tag-link-267' title='27个话题' style='font-size: 14px;'>数学之美</a>
<a href='http://www.code123.cc/tag/programmer' class='tag-link-176' title='22个话题' style='font-size: 14px;'>程序员</a>
<a href='http://www.code123.cc/tag/interview' class='tag-link-89' title='20个话题' style='font-size: 14px;'>面试/求职</a>
<a href='http://www.code123.cc/tag/resource-share' class='tag-link-82' title='19个话题' style='font-size: 14px;'>资源分享</a>
<a href='http://www.code123.cc/tag/probability-theory' class='tag-link-195' title='18个话题' style='font-size: 14px;'>概率论</a>
<a href='http://www.code123.cc/tag/bigdata' class='tag-link-269' title='17个话题' style='font-size: 14px;'>大数据</a>
<a href='http://www.code123.cc/tag/tcpip' class='tag-link-205' title='16个话题' style='font-size: 14px;'>TCP/IP</a>
<a href='http://www.code123.cc/tag/algorithm' class='tag-link-5' title='14个话题' style='font-size: 14px;'>算法</a>
<a href='http://www.code123.cc/tag/java-level01' class='tag-link-173' title='13个话题' style='font-size: 14px;'>Java基础</a>
<a href='http://www.code123.cc/tag/java' class='tag-link-165' title='13个话题' style='font-size: 14px;'>Java</a>
<a href='http://www.code123.cc/tag/carrier' class='tag-link-118' title='13个话题' style='font-size: 14px;'>职场</a>
<a href='http://www.code123.cc/tag/spark' class='tag-link-135' title='12个话题' style='font-size: 14px;'>spark</a>
<a href='http://www.code123.cc/tag/python' class='tag-link-58' title='12个话题' style='font-size: 14px;'>Python</a>
<a href='http://www.code123.cc/tag/google' class='tag-link-125' title='12个话题' style='font-size: 14px;'>谷歌</a>
<a href='http://www.code123.cc/tag/system-architecture' class='tag-link-143' title='11个话题' style='font-size: 14px;'>系统架构</a>        </div>
    </section>
<section></section><h1>精彩推荐</h1><ul><li class="favorites"><i class='fa fa-star'></i>&nbsp&nbsp<a href='http://www.code123.cc/281.html' title='Spark Streaming初探'>Spark Streaming初探</a> (1)</li><li class="favorites"><i class='fa fa-star'></i>&nbsp&nbsp<a href='http://www.code123.cc/121.html' title='中国互联网走在“灰度”上'>中国互联网走在“灰度”上</a> (1)</li><li class="favorites"><i class='fa fa-star'></i>&nbsp&nbsp<a href='http://www.code123.cc/255.html' title='Python正则表达式指南'>Python正则表达式指南</a> (1)</li><li class="favorites"><i class='fa fa-star'></i>&nbsp&nbsp<a href='http://www.code123.cc/253.html' title='当开发者产生一个伟大的想法之后应该做的10件事'>当开发者产生一个伟大的想法之后应该做的10件事</a> (1)</li><li class="favorites"><i class='fa fa-star'></i>&nbsp&nbsp<a href='http://www.code123.cc/250.html' title='互联网成功十大案例'>互联网成功十大案例</a> (1)</li><li class="favorites"><i class='fa fa-star'></i>&nbsp&nbsp<a href='http://www.code123.cc/1256.html' title='自己动手制作一台四位计算机'>自己动手制作一台四位计算机</a> (1)</li><li class="favorites"><i class='fa fa-star'></i>&nbsp&nbsp<a href='http://www.code123.cc/258.html' title='哈希表'>哈希表</a> (1)</li><li class="favorites"><i class='fa fa-star'></i>&nbsp&nbsp<a href='http://www.code123.cc/118.html' title='留几手：哎呦卧槽，这原来就是互联网创业啊'>留几手：哎呦卧槽，这原来就是互联网创业啊</a> (1)</li><li class="favorites"><i class='fa fa-star'></i>&nbsp&nbsp<a href='http://www.code123.cc/320.html' title='Android开发中很有用的方法、类与接口收集'>Android开发中很有用的方法、类与接口收集</a> (1)</li><li class="favorites"><i class='fa fa-star'></i>&nbsp&nbsp<a href='http://www.code123.cc/146.html' title='移动web开发资源整理'>移动web开发资源整理</a> (1)</li></ul></li>
<div id="sidebar-fixed"></div>
			
</aside>
	</div>
</div>
<footer id="footer">
	<div class="sns">
		<a href="http://weibo.com/u/5603150440" target="_blank" rel="nofollow" class="weibo"><span>新浪微博</span></a><a href="http://www.zhihu.com/people/code123" target="_blank" rel="nofollow" class="zhihu"><span>知乎</span></a><a href="http://www.code123.cc/feed" target="_blank" rel="nofollow" class="rss"><span>订阅我</span></a>	</div>
	<div class="copyright">Copyright &copy; 2015 <a href="/">Code123</a> All Rights Reserved <a href="http://www.code123.cc/contact" rel="nofollow">Contact</a></div>
    <div class="statistics-div"><script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1253876422'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s95.cnzz.com/z_stat.php%3Fid%3D1253876422%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));</script></div>
</footer>
<script type='text/javascript' src='http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/plugins/akismet/_inc/form.js?ver=1462685583'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var viewsCacheL10n = {"admin_ajax_url":"http:\/\/www.code123.cc\/wp-admin\/admin-ajax.php","post_id":"2657"};
/* ]]> */
</script>
<script type='text/javascript' src='http://7xlqnq.com1.z0.glb.clouddn.com/wp-content/plugins/wp-postviews/postviews-cache.js?ver=1462685583'></script>

</body>
</html>

<!-- Dynamic page generated in 0.441 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2017-03-30 13:59:08 -->

<!-- Compression = gzip -->